package com.example.testmysql.controller;

import com.example.testmysql.entity.JSONObject;
import com.example.testmysql.entity.TBook;
import com.example.testmysql.service.BookService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BookController {

    @Resource
    private BookService bookService;


    /**
     * 新增书籍
     * @param book
     * @return
     */
    @RequestMapping(value = "/upload")
    public ResponseEntity<JSONObject> save(@RequestBody TBook book){
        JSONObject jo = new JSONObject();

        if (book.getBookName() == null || "".equals(book.getBookName())){
            jo.setCode("-1");
            jo.setMsg("book_name不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        if (book.getPrice() == 0){
            jo.setCode("-1");
            jo.setMsg("price不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        if (book.getPublisher() == null || "".equals(book.getPublisher())){
            jo.setCode("-1");
            jo.setMsg("publisher不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }


        TBook exist = bookService.queryBookByBookName(book.getBookName());
        if (exist != null){
            jo.setCode("-1");
            jo.setMsg("book_name已存在，请更改");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }


        book.setStatus(1);
        bookService.save(book);

        jo.setCode("1");
        jo.setMsg("上传书籍成功");
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);

    }


    /**
     * 根据书名查询书籍是否存在
     * @param book
     * @return
     */
    @RequestMapping(value = "/queryBookByName")
    public ResponseEntity<JSONObject> queryBookByName(@RequestBody TBook book) {
        JSONObject jo = new JSONObject();

        // 检查书名是否为空
        if (book.getBookName() == null || "".equals(book.getBookName())) {
            jo.setCode("-1");
            jo.setMsg("bookName不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        // 调用Service层方法，根据书名查询书籍
        TBook exist = bookService.queryBookByBookName(book.getBookName());

        // 判断查询结果是否为空
        if (exist == null) {
            jo.setCode("-1");
            jo.setMsg("没有找到对应的书籍");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        // 返回查询成功结果
        jo.setCode("1");
        jo.setMsg("查询成功");
        jo.setO(exist);
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }

    /**
     * 根据出版社查询书籍
     * @param book
     * @return
     */
    @RequestMapping(value = "/queryBookByPublisher")
    public ResponseEntity<JSONObject> queryBookByPublisher(@RequestBody TBook book){
        JSONObject jo = new JSONObject();

        if (book.getPublisher() == null || "".equals(book.getPublisher())){
            jo.setCode("-1");
            jo.setMsg("publisher不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        // 调用service层的方法，根据出版社查询书籍
        List<TBook> books = bookService.queryBookByPublisher(book.getPublisher());

        // 判断查询结果是否为空
        if (books == null || books.isEmpty()) {
            jo.setCode("-1");
            jo.setMsg("没有找到对应的书籍");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }


        jo.setCode("1");
        jo.setMsg("查询成功");
        jo.setO(books);
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }



    /**
     * 借出书籍
     * @param book
     * @return
     */
    @RequestMapping(value = "/deleteBook")
    public ResponseEntity<JSONObject> deleteBook(@RequestBody TBook book) {
        JSONObject jo = new JSONObject();

        if (book.getBookName() == null || "".equals(book.getBookName())){
            jo.setCode("-1");
            jo.setMsg("bookName不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        TBook exist = bookService.queryBookByBookName(book.getBookName());
        if (exist == null){
            jo.setCode("-1");
            jo.setMsg("bookName不存在，请先上传书籍");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        bookService.deleteBook(book.getBookName());

        jo.setCode("1");
        jo.setMsg("借出成功");
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }
}
